/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * jifReporteSuministros.java
 *
 * Created on 28-oct-2010, 15:48:41
 */

package GestionMovimientos;

import administracion.delegate.GestionPedidosDelegate;
import administracion.helper.Listas;
import administracion.helper.Utiles_Cadena;
import administracion.helper.Utiles_MensajesDialogo;
import administracion.propiedades.Propiedades;
import com.jidesoft.hints.ListDataIntelliHints;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.view.JasperViewer;
import suministros.ejb30.TO.AnoTO;
import suministros.ejb30.TO.EmpresaTO;
import suministros.ejb30.TO.SuministroTO;
import suministros.ejb30.TO.TipoTO;

/**
 *
 * @author Diana
 */
public class jifKardexSuministro extends javax.swing.JInternalFrame {
    private ListDataIntelliHints intelliHintsSuministros;
    SuministroTO suministro;
    List<EmpresaTO> empresas;
    List<AnoTO> anomes= new ArrayList<AnoTO>();
    /** Creates new form jifReporteSuministros */
    public jifKardexSuministro() {
        initComponents();     
        this.cargaHintsIndependientes();      
        this.llenarMes();
        this.pack();
        
    }
    
  
    private void cargaHintsIndependientes() {
    this.intelliHintsSuministros = new ListDataIntelliHints(this.jtfDescSuministro, Listas.getSuministros());
  }
     private void buscarSuministros()
  {
    this.suministro = null;
    SuministroTO sumi;
    if (this.jtfCodSuministro.getText().trim().length() < 1) {

      try { sumi = (SuministroTO)this.intelliHintsSuministros.getSelectedHint();
      } catch (NullPointerException ex) {
        sumi = null;
      }
    } else {
      sumi = Listas.getSuministroCodigo(this.jtfCodSuministro.getText().trim());
    }

    if (sumi == null) {
      Utiles_MensajesDialogo.mensajeError("El suministro no existe!");
      this.jtfDescSuministro.setText("");
     } else {
      this.jtfDescSuministro.setText(sumi.toString());
      this.jtfCodSuministro.setText(sumi.getId_suministro());
      this.suministro = sumi;
    }
  }

   public void GenerarReporte(){      
        JasperViewer jv = null;
        JasperPrint jp = null;
            try {
            Map parametros = new HashMap();
            String su=this.jtfCodSuministro.getText().trim();
            int mes= Utiles_Cadena.getMesAnioT(this.jcbMes.getSelectedItem().toString().trim());
            parametros.put("anio", this.jcbAnio.getSelectedItem().toString().trim());
            parametros.put("mes", mes+"");
            parametros.put("id_empresa","001");
            parametros.put("id_usuario", Propiedades.getUsuario());
            parametros.put("suministro", su);

            jp = GestionPedidosDelegate.getInstance().generarReporte("ReporteKardexSuministroOperativo.jasper", parametros);
            jv = new JasperViewer(jp, false);
            jv.setExtendedState(javax.swing.JFrame.MAXIMIZED_BOTH);
            jv.setVisible(true);
        } catch (Exception e) {
            Utiles_MensajesDialogo.mensajeError("No se pudo realizar la transaccion"+e.getMessage());
        }
  }
   public void llenarMes(){
        try {
            anomes = GestionPedidosDelegate.getInstance().getParametrosKardex();
        } catch (Exception ex) {
           Utiles_MensajesDialogo.mensajeAlerta("No se han registrado ingresos o salidas de suministros en la empresa");
        }
        for(int i=0; i<anomes.size(); i++){
           if(buscarAnio(anomes.get(i).getAno()))
           this.jcbAnio.addItem(anomes.get(i).getAno());
        }
         for(int i=0; i<anomes.size(); i++){
             String mes=Utiles_Cadena.getMesAnio(anomes.get(i).getMes());
             if(buscarMes(mes))
                this.jcbMes.addItem(mes);
        }

     }
   public boolean buscarAnio(String anio){
         boolean encontrado=true;
         for(int i=0; i<this.jcbAnio.getItemCount(); i++){
             if(this.jcbAnio.getItemAt(i).toString().compareTo(anio)==0){
                 encontrado=false;
             }
         }
         return encontrado;
     }
     public boolean buscarMes(String mes){
         boolean encontrado=true;
         for(int i=0; i<this.jcbMes.getItemCount(); i++){
             if(this.jcbMes.getItemAt(i).toString().compareTo(mes)==0){
                 encontrado=false;
             }
         }
         return encontrado;
     }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cboBeanBindingTipo1 = new administracion.BeanBinding.CboBeanBindingTipo();
        cboBeanBindingEmpresa1 = new administracion.BeanBinding.CboBeanBindingEmpresa();
        buttonGroup1 = new javax.swing.ButtonGroup();
        jbBuscar = new javax.swing.JButton();
        jbCancelar = new javax.swing.JButton();
        jcbAnio = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jcbMes = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jtfCodSuministro = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jtfDescSuministro = new javax.swing.JTextField();

        setTitle("REPORTE DE SUMINISTROS");

        jbBuscar.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jbBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16ozygen/actions/format_justify_fill.png"))); // NOI18N
        jbBuscar.setMnemonic('B');
        jbBuscar.setText("VER REPORTE");
        jbBuscar.setToolTipText("Cancelar la Operacion");
        jbBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbBuscarActionPerformed(evt);
            }
        });
        jbBuscar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jbBuscarKeyPressed(evt);
            }
        });

        jbCancelar.setFont(new java.awt.Font("Arial", 1, 12));
        jbCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16ozygen/actions/button_cancel.png"))); // NOI18N
        jbCancelar.setMnemonic('C');
        jbCancelar.setText("CANCELAR");
        jbCancelar.setToolTipText("Cancelar la Operacion");
        jbCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCancelarActionPerformed(evt);
            }
        });
        jbCancelar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jbCancelarKeyPressed(evt);
            }
        });

        jcbAnio.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jcbAnioKeyPressed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jLabel3.setText("AÑO:");

        jcbMes.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jcbMesKeyPressed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jLabel1.setText("MES:");

        jLabel9.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jLabel9.setText("COD.SUMINISTRO");

        jtfCodSuministro.setFont(new java.awt.Font("Arial 12 Simple 12 Simple", 0, 12)); // NOI18N
        jtfCodSuministro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtfCodSuministroActionPerformed(evt);
            }
        });

        jLabel10.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jLabel10.setText("DESC.SUMINISTRO");

        jtfDescSuministro.setFont(new java.awt.Font("Arial 12 Simple 12 Simple", 0, 12)); // NOI18N
        jtfDescSuministro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtfDescSuministroActionPerformed(evt);
            }
        });
        jtfDescSuministro.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jtfDescSuministroKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(10, 10, 10)
                                .addComponent(jcbMes, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel3)
                                .addGap(34, 34, 34)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jcbAnio, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel9)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jtfCodSuministro, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jtfDescSuministro, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(131, 131, 131)
                        .addComponent(jbBuscar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(30, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jcbMes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel1))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jcbAnio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel3)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(jtfCodSuministro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10)
                    .addComponent(jtfDescSuministro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbBuscar)
                    .addComponent(jbCancelar)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbBuscarActionPerformed
      this.GenerarReporte();

    }//GEN-LAST:event_jbBuscarActionPerformed

    private void jbBuscarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jbBuscarKeyPressed


}//GEN-LAST:event_jbBuscarKeyPressed

    private void jbCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCancelarActionPerformed

        this.dispose();
}//GEN-LAST:event_jbCancelarActionPerformed

    private void jbCancelarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jbCancelarKeyPressed

}//GEN-LAST:event_jbCancelarKeyPressed

    private void jtfCodSuministroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtfCodSuministroActionPerformed

        if (this.jtfCodSuministro.getText().trim().length() < 1)
            this.jtfDescSuministro.requestFocus();
        else
            buscarSuministros();
}//GEN-LAST:event_jtfCodSuministroActionPerformed

    private void jtfDescSuministroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtfDescSuministroActionPerformed

        buscarSuministros();
}//GEN-LAST:event_jtfDescSuministroActionPerformed

    private void jtfDescSuministroKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfDescSuministroKeyPressed

        if (this.jtfCodSuministro.getText().trim().length() > 0)
            this.jtfCodSuministro.setText("");
}//GEN-LAST:event_jtfDescSuministroKeyPressed

    private void jcbMesKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jcbMesKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER ){
            this.jcbAnio.requestFocus();
        }
}//GEN-LAST:event_jcbMesKeyPressed

    private void jcbAnioKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jcbAnioKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER ){
        }
}//GEN-LAST:event_jcbAnioKeyPressed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private administracion.BeanBinding.CboBeanBindingEmpresa cboBeanBindingEmpresa1;
    private administracion.BeanBinding.CboBeanBindingTipo cboBeanBindingTipo1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JButton jbBuscar;
    private javax.swing.JButton jbCancelar;
    private javax.swing.JComboBox jcbAnio;
    private javax.swing.JComboBox jcbMes;
    private javax.swing.JTextField jtfCodSuministro;
    private javax.swing.JTextField jtfDescSuministro;
    // End of variables declaration//GEN-END:variables

}
